package team.pra.manager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.pra.manager.common.ManagerResult;
import team.pra.manager.common.PageBean;
import team.pra.manager.common.PageHelper;
import team.pra.manager.mapper.RoomMapper;
import team.pra.manager.pojo.Room;
import team.pra.manager.service.RoomService;

import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired(required = false)
    private RoomMapper roomMapper;

    public PageHelper findRoomByPage(Integer code, Integer size){
        PageBean<Room> roomPage = new PageBean<>(code,size);

        List<Room> rooms = roomMapper.findRoomByPage(roomPage);
        Integer totalRooms = roomMapper.countRoomNumber();

        roomPage.setTotal(totalRooms);
        roomPage.setRows(rooms);

        PageHelper pageHelper = new PageHelper();
        pageHelper.setData(rooms);
        pageHelper.setRecordsTotal(totalRooms);
        pageHelper.setRecordsFiltered(totalRooms);
        return pageHelper;
    }

    @Override
    public void findRoomByCondition(PageHelper<Room> pageHelper) {
        pageHelper.setData(roomMapper.findRoomByCondition(pageHelper));
        pageHelper.setRecordsFiltered(roomMapper.filterRoomCount(pageHelper));
        pageHelper.setRecordsTotal(roomMapper.countRoomNumber());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ManagerResult insertRoom(Room room) {
        boolean flag = roomMapper.insertRoom(room);
        return ManagerResult.ok(flag);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ManagerResult updateRoom(Room room) {
        boolean flag = roomMapper.updateRoom(room);
        return ManagerResult.ok(flag);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ManagerResult deleteRoom(Integer id) {
        boolean flag = roomMapper.deleteRoom(id);
        return ManagerResult.ok(flag);
    }

    @Override
    public List<Room> findAllRooms() {
        return roomMapper.findAllRoom();
    }
}
